﻿using System.Collections.Generic;
using System.Web.Mvc;
using InvoiceInvoker.Logic;
using InvoiceInvoker.Logic.Repositories;
using InvoiceInvoker.MvcUI.Helpers;
using InvoiceInvoker.MvcUI.Models;
using InvoiceInvoker.MvcUI.ViewModels;

namespace InvoiceInvoker.MvcUI.Controllers
{
	[Authorize]
	[HandleError]
	public class PatternsController : Controller
	{
		private int _sellerId;
		private InvoicePatternRepository _patternRepository;
		private RegisteredCustomerRepository _customerRepository;
		private RegisteredProductRepository _productRepository;

		protected override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			this.SetSellerId();
			_sellerId = (int)Session["SellerId"];
			_patternRepository = new InvoicePatternRepository(_sellerId);
			_customerRepository = new RegisteredCustomerRepository(_sellerId);
			_productRepository = new RegisteredProductRepository(_sellerId);

			base.OnActionExecuting(filterContext);
		}

		//
		// GET: /Patterns/

		public ActionResult Index(PatternsIndexViewModel viewModel)
		{
			if (viewModel.Filter == null)
				viewModel.Filter = new Filters.PatternsFilter();

			viewModel.Patterns = _patternRepository.GetByExpression(viewModel.Filter.GetExpression()).ToPatternModelList();

			return View(viewModel);
		}

		//
		// GET: /Patterns/Details/5

		public ActionResult Details(int id)
		{
			PatternModel pattern = new PatternModel(_patternRepository.GetById(id));

			return View(pattern);
		}

		//
		// GET: /Patterns/Create

		public ActionResult Create()
		{
			List<int> productIds = new List<int>();

			Session["ProductIds"] = productIds;

			PatternsViewModel viewModel = new PatternsViewModel
			{
				Pattern = new PatternModel(_sellerId),
				Customers = _customerRepository.GetAll().ToCustomerModelList(),
				PaymentTypes = PaymentTypesProvider.GetPaymentTypes()
			};

			return View(viewModel);
		}

		//
		// POST: /Patterns/Create

		[HttpPost]
		public ActionResult Create(PatternsViewModel viewModel)
		{
			List<int> productIds = Session["ProductIds"] as List<int>;

			if (ModelState.IsValid && productIds.Count > 0)
			{
				_patternRepository.Add(viewModel.Pattern.ToInvoicePattern(), productIds);
				Session["ProductIds"] = null;

				return RedirectToAction("Index");
			}
			else
			{
				if (productIds.Count == 0)
					ViewData["ErrorMessage"] = "Wybierz produkty";
				else
				{
					foreach (int productId in productIds)
						viewModel.Pattern.Products.Add(new ProductModel(_productRepository.GetById(productId)));
				}

				viewModel.Customers = _customerRepository.GetAll().ToCustomerModelList();
				viewModel.PaymentTypes = PaymentTypesProvider.GetPaymentTypes();

				return View(viewModel);
			}
		}

		//
		// GET: /Patterns/Edit/5

		public ActionResult Edit(int id)
		{
			PatternModel pattern = new PatternModel(_patternRepository.GetById(id));

			List<int> productIds = new List<int>();
			pattern.Products.ForEach(product => productIds.Add(product.Id));

			Session["ProductIds"] = productIds;

			PatternsViewModel viewModel = new PatternsViewModel()
			{
				Pattern = pattern,
				Customers = _customerRepository.GetAll().ToCustomerModelList(),
				PaymentTypes = PaymentTypesProvider.GetPaymentTypes()
			};

			return View(viewModel);
		}

		//
		// POST: /Patterns/Edit/5

		[HttpPost]
		public ActionResult Edit(int id, PatternsViewModel viewModel)
		{
			List<int> productIds = Session["ProductIds"] as List<int>;

			if (ModelState.IsValid && productIds.Count > 0)
			{
				_patternRepository.Update(viewModel.Pattern.ToInvoicePattern(), productIds);
				Session["ProductIds"] = null;

				return RedirectToAction("Index");
			}
			else
			{
				if (productIds.Count == 0)
					ViewData["ErrorMessage"] = "Wybierz produkty";
				else
				{
					foreach (int productId in productIds)
						viewModel.Pattern.Products.Add(new ProductModel(_productRepository.GetById(productId)));
				}

				viewModel.Customers = _customerRepository.GetAll().ToCustomerModelList();
				viewModel.PaymentTypes = PaymentTypesProvider.GetPaymentTypes();

				return View(viewModel);
			}
		}

		//
		// GET: /Patterns/Delete/5

		public ActionResult Delete(int id)
		{
			PatternModel pattern = new PatternModel(_patternRepository.GetById(id));

			return View(pattern);
		}

		//
		// POST: /Patterns/Delete/5

		[HttpPost]
		public ActionResult Delete(int id, FormCollection collection)
		{
			_patternRepository.Remove(id);

			return RedirectToAction("Index");
		}
	}
}
